package com.wkk.entity;

import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionBindingListener;
import javax.xml.stream.events.Comment;
import java.util.List;
import java.util.Map;

public class User implements HttpSessionBindingListener {
    private Integer uid;//编号
    private String username;//用户名
    private String password;//密码
    private String picture;//头像路径
    private String mobile;//手机号码
    private String createTime;//创建时间
    private String level;//用户级别
    private Integer cid;//教练编号
    private String nickname;//昵称
    private String myself;//自拍照
    private String getCidTime;//与教练链接时间
    private String sex;//性别
    private Integer cidState;//用于用户确认教练

    private Integer fanCount;//粉丝数
    private Integer chatCount;//朋友圈数
    private Integer fanTrue;//判断查看用户时，是否已经关注该对象
    private List<ChatRecord> chatRecords;//自己的所有朋友圈

    private String startTime;//用于精确查询
    private String endTime;//用于精确查询

    private String endMember;//会员到期时间

    private Coach coach;

    public User() {
    }

    public User(String username, String mobile) {
        this.username = username;
        this.mobile = mobile;
    }



    //将Java对象与session绑定
    //将对象存入session域中
    @Override
    public void valueBound(HttpSessionBindingEvent event) {
        //通过事件对象获取事件源对象
        HttpSession session = event.getSession();
        Map<User, HttpSession> userMap = (Map<User, HttpSession>) session
                .getServletContext().getAttribute("userMap");
        //将用户存入map中
        userMap.put(this, session);
    }
    // 当session和对象解除绑定的时候
    @Override
    public void valueUnbound(HttpSessionBindingEvent event) {
        //获取session对象
        HttpSession session = event.getSession();
        // 获得人员列表
        Map<User, HttpSession> userMap = (Map<User, HttpSession>) session
                .getServletContext().getAttribute("userMap");
        // 将用户移除了
        userMap.remove(this);
    }

    //重写hashCode和equals实现：同一个id等于同一个用户
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + uid;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        User other = (User) obj;
        if (uid != other.uid)
            return false;
        return true;
    }

    public Integer getCidState() {
        return cidState;
    }

    public void setCidState(Integer cidState) {
        this.cidState = cidState;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getGetCidTime() {
        return getCidTime;
    }

    public void setGetCidTime(String getCidTime) {
        this.getCidTime = getCidTime;
    }

    public Integer getFanTrue() {
        return fanTrue;
    }

    public void setFanTrue(Integer fanTrue) {
        this.fanTrue = fanTrue;
    }

    public List<ChatRecord> getChatRecords() {
        return chatRecords;
    }

    public void setChatRecords(List<ChatRecord> chatRecords) {
        this.chatRecords = chatRecords;
    }

    public Integer getFanCount() {
        return fanCount;
    }

    public void setFanCount(Integer fanCount) {
        this.fanCount = fanCount;
    }

    public Integer getChatCount() {
        return chatCount;
    }

    public void setChatCount(Integer chatCount) {
        this.chatCount = chatCount;
    }

    public Integer getUid() {
        return uid;
    }

    public void setUid(Integer uid) {
        this.uid = uid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getPicture() {
        return picture;
    }

    public void setPicture(String picture) {
        this.picture = picture;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public String getCreateTime() {
        return createTime;
    }

    public void setCreateTime(String createTime) {
        this.createTime = createTime;
    }

    public String getLevel() {
        return level;
    }

    public void setLevel(String level) {
        this.level = level;
    }

    public Integer getCid() {
        return cid;
    }

    public void setCid(Integer cid) {
        this.cid = cid;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getMyself() {
        return myself;
    }

    public void setMyself(String myself) {
        this.myself = myself;
    }

    public String getEndMember() {
        return endMember;
    }

    public void setEndMember(String endMember) {
        this.endMember = endMember;
    }

    public Coach getCoach() {
        return coach;
    }

    public void setCoach(Coach coach) {
        this.coach = coach;
    }

    @Override
    public String toString() {
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", picture='" + picture + '\'' +
                ", mobile='" + mobile + '\'' +
                ", createTime='" + createTime + '\'' +
                ", level='" + level + '\'' +
                ", cid=" + cid +
                ", nickname='" + nickname + '\'' +
                ", myself='" + myself + '\'' +
                ", getCidTime='" + getCidTime + '\'' +
                ", sex='" + sex + '\'' +
                ", fanCount=" + fanCount +
                ", chatCount=" + chatCount +
                ", fanTrue=" + fanTrue +
                ", chatRecords=" + chatRecords +
                ", startTime='" + startTime + '\'' +
                ", endTime='" + endTime + '\'' +
                ", endMember='" + endMember + '\'' +
                ", coach=" + coach +
                '}';
    }
}
